package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.UserDao;
import com.itheima.domain.system.User;
import com.itheima.factory.MapperFactory;
import com.itheima.service.system.UserService;
import com.itheima.utils.ArrayToListUtil;
import com.itheima.utils.MD5Util;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class UserServiceImpl implements UserService {
    @Override
    public List<User> findAll() {
        SqlSession sqlSession = null;
        List<User> all = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = MapperFactory.getMapper(sqlSession, UserDao.class);
            all = mapper.findAll();
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {

            TransactionUtil.close(sqlSession);
        }
        return all;
    }

    @Override
    public PageInfo findAll(Integer page, Integer size) {
        SqlSession sqlSession = null;
        PageInfo<User> userPageInfo = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = MapperFactory.getMapper(sqlSession, UserDao.class);
            PageHelper.startPage(page, size);
            List<User> all = mapper.findAll();
            userPageInfo = new PageInfo<>(all);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {

            TransactionUtil.close(sqlSession);
        }
        return userPageInfo;
    }

    @Override
    public User findById(String id) {
        SqlSession sqlSession = null;
        User user = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = MapperFactory.getMapper(sqlSession, UserDao.class);
            user = mapper.findById(id);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }
        return user;
    }

    @Override
    public void delete(String...id) {
        SqlSession sqlSession = null;

        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = MapperFactory.getMapper(sqlSession, UserDao.class);
            mapper.delete(id);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public void save(User user) {
        SqlSession sqlSession = null;

        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = MapperFactory.getMapper(sqlSession, UserDao.class);
            user.setPassword(MD5Util.md5(user.getPassword()));
            user.setId(UUID.randomUUID().toString());
            user.setState((long) 1);
            mapper.save(user);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }


    }

    @Override
    public void update(User user) {
        SqlSession sqlSession = null;

        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = MapperFactory.getMapper(sqlSession, UserDao.class);
            user.setPassword(MD5Util.md5(user.getPassword()));
            mapper.update(user);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void updateRole(String userId, String[] roleIds) {

        SqlSession sqlSession = null;

        try {
            //清除原有绑定

            //添加当前绑定
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = MapperFactory.getMapper(sqlSession, UserDao.class);
           mapper.deleteRole(userId);
           mapper.addRole(userId,roleIds);

            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public User login(String email, String password) {
        SqlSession sqlSession = null;
        User user = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = MapperFactory.getMapper(sqlSession, UserDao.class);
            password= MD5Util.md5(password);
            user = mapper.findByEmailAndPassword(email,password);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }
        return user;
    }
}
